package com.bestv.datalake.controller;

import com.bestv.datalake.exception.ExceptionResponse;
import com.bestv.datalake.exception.LogRuleConflictException;
import com.bestv.datalake.exception.LogRuleExistException;
import com.bestv.datalake.exception.LogRuleNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lukun on 16-7-21.
 */
@ControllerAdvice
public class LogRulerControllerAdvice {

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(LogRuleNotFoundException.class)
    @ResponseBody
    public ExceptionResponse handleNotFoundException(HttpServletRequest req, LogRuleNotFoundException ex) {
        return new ExceptionResponse(System.currentTimeMillis(),
                HttpStatus.NOT_FOUND.value(),
                "日志解析规则没有找到",
                "LogRuleNotFoundException",
                ex.getMessage(),
                req.getRequestURL().toString());
    }

    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(LogRuleExistException.class)
    @ResponseBody
    public ExceptionResponse handleExistException(HttpServletRequest req, LogRuleExistException ex) {
        return new ExceptionResponse(System.currentTimeMillis(),
                HttpStatus.CONFLICT.value(),
                "日志解析规则已经存在",
                "LogRuleExistException",
                ex.getMessage(),
                req.getRequestURL().toString());
    }

    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(LogRuleConflictException.class)
    @ResponseBody
    public ExceptionResponse handleConflictException(HttpServletRequest req, LogRuleConflictException ex) {
        return new ExceptionResponse(System.currentTimeMillis(),
                HttpStatus.CONFLICT.value(),
                "日志解析规则新旧业务键或代理键不一致",
                "LogRuleConflictException",
                ex.getMessage(),
                req.getRequestURL().toString());
    }

//    @ResponseStatus(HttpStatus.NO_CONTENT)
//    @ExceptionHandler(LogRuleConflictException.class)
//    @ResponseBody
//    public ExceptionResponse handleConflictException(HttpServletRequest req, LogRuleConflictException ex) {
//        return new ExceptionResponse(System.currentTimeMillis(),
//                HttpStatus.CONFLICT.value(),
//                "日志解析规则新旧业务键或代理键不一致",
//                "LogRuleConflictException",
//                ex.getMessage(),
//                req.getRequestURL().toString());
//    }

}
